#------------------------------------------------------------------------------
rm(list = ls())
library(LalRUtils)
libreq(
  data.table, zoo, tictoc, fixest, PanelMatch, patchwork,
  rio, magrittr, janitor, did, panelView, ggplot2, RPushbullet, ggiplot, 
  tidyverse, data.table, zoo, tictoc, fst, fixest, PanelMatch, patchwork,
  rio, magrittr, janitor, did, panelView, ggiplot, tictoc, binsreg, interflex
)

set.seed(42)
theme_set(lal_plot_theme())

notif = \(x) pbPost("note", x)

#------------------------------------------------------------------------------



#------------------------------------------------------------------------------
# Define Paths
#------------------------------------------------------------------------------

# R studio
setwd( dirname(rstudioapi::getActiveDocumentContext()$path) )
# R default : unccoment if you use default R
# setwd(getSrcDirectory(function(){})[1])
#------------------------------------------------------------------------------



#------------------------------------------------------------------------------
# Load Data
#------------------------------------------------------------------------------

vcf <- fread("vcf_data_complete.csv", sep = ",")
setnames( vcf, "d", "D")
vcf_data <- copy( vcf )
gfc <- fread("gfc_dta.csv" )

#------------------------------------------------------------------------------




#------------------------------------------------------------------------------
# Import data
#------------------------------------------------------------------------------

# %%
kv = c("def_ha", "sch", "D", "village", "state", "year", "styear",
       "t", "block", "pref", "pref_bin", "pesa_exposure",
       "min_dist_to_mine", "pref_mean")
gfc = gfc[, ..kv]



sumvars = c('def_ha', 'sch', 'pref_mean')
summvars = c('notNA(x)', 'mean(x)', 'sd(x)', 'min(x)', 'pctile(x)[25]', 'median(x)', 'pctile(x)[75]', 'max(x)')
labs = c('Deforested Area (Hectares)',
         'Scheduled Status', 'Ex-ante forest cover in 2000 (ex-ante)'
)
st(gfc[, ..sumvars], summ = summvars, digits = 5, 
   factor.percent = FALSE, factor.counts = FALSE,
   labels = labs, title = "Summary Statistics (GFC Data) - full sample",
   file = "appendix_tableA3.tex", out = 'latex')

#------------------------------------------------------------------------------